package com.example;

import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;
import javafx.application.Platform;

import java.util.HashSet;
import java.util.Set;

/**
 * <p>全局键盘监听器</p>
 *
 * @author dc
 * @date 2025-09-16
 **/
public class GlobalKeyListener implements NativeKeyListener {
    private boolean altPressed = false;

    @Override
    public void nativeKeyPressed(NativeKeyEvent nativeEvent) {
        // 记录Alt键按下状态
        if (nativeEvent.getKeyCode() == NativeKeyEvent.VC_ALT) {
            altPressed = true;
        }
        if (nativeEvent.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
            System.out.println("检测到 escape 键");
            // 触发截图事件
            KeyEventBroadCast.getInstance().closeScreenShot();
        }

        // 检测Alt+X组合键
        if (altPressed && nativeEvent.getKeyCode() == NativeKeyEvent.VC_X) {
            System.out.println("检测到 Alt+X 组合键");
            // 触发截图事件
            KeyEventBroadCast.getInstance().triggerScreenShot();
        }
        
    }

    @Override
    public void nativeKeyReleased(NativeKeyEvent nativeEvent) {
        // 释放Alt键状态
        if (nativeEvent.getKeyCode() == NativeKeyEvent.VC_ALT) {
            altPressed = false;
        }
    }

    // 只有当按键产生实际字符输入时才触发
    @Override
    public void nativeKeyTyped(NativeKeyEvent nativeEvent) {
        // 只有能产生字符的按键才会触发
        // 例如：按下A键（产生字符'a'）会触发
        // 但按下Ctrl、Shift、Alt等功能键不会触发
    }
}
